package m.html.element.other;

import m.common.model.Model;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.common.model.util.QueryPage;
import m.system.util.ClassUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.util.DictionaryUtil;

import java.util.*;

public class ItemsQuery {
	
	private Class<Model> clazz;
	private String title;
	private String titleExpression;
	private String value;
	private QueryOrder[] order;
	private QueryPage page;
	private List<QueryCondition> cond;
	
	private String dictType;
	
	/**
	 * 类查询构造
	 *  clazz
	 *  title
	 *  value
	 */
	public <T extends Model> ItemsQuery(Class<T> clazz,String title,String value) {
		this.clazz=(Class<Model>) clazz;
		this.title=title;
		this.value=value;
		this.page=null;
		this.cond= new ArrayList<>();
	}
	/**
	 * 字典构造
	 *  dictType
	 */
	public ItemsQuery(String dictType) {
		this.dictType=dictType;
	}
	public ItemsQuery setTitleExpression(String titleExpression) {
		this.titleExpression = titleExpression;
		return this;
	}
	public ItemsQuery setOrder(QueryOrder... order) {
		this.order = order;
		return this;
	}
	public ItemsQuery setPage(QueryPage page) {
		this.page = page;
		return this;
	}
	public ItemsQuery addCond(QueryCondition... cond) {
		this.cond.addAll(Arrays.asList(cond));
		return this;
	}
	/**
	 * 转换成 [{key:value,value:value,label:name}...]
	 * 
	 *  Exception
	 */
	public List<JSONMessage> toDatas() throws Exception{
		List<JSONMessage> items= new ArrayList<>();
		if(!StringUtil.isSpace(dictType)) {
			Map<String,String> map=DictionaryUtil.get(dictType);
			for(String key : map.keySet()) {
				JSONMessage data=new JSONMessage();
				data.push("key", key);
				data.push("value", key);
				data.push("label", map.get(key));
				items.add(data);
			}
		}else {
			Map<String,String> map= new HashMap<>();
			if(!StringUtil.isSpace(this.titleExpression)) map.put(this.title,this.titleExpression);
			List<Model> models=ModelQueryList.getModelList(clazz, 
				new String[] {this.title,this.value},page,
                    !cond.isEmpty() ?QueryCondition.and(cond.toArray(new QueryCondition[]{})):null,
				map,order);
			for(Model model : models) {
				JSONMessage data=new JSONMessage();
				data.push("key", ClassUtil.getFieldValue(model, this.value));
				data.push("value", ClassUtil.getFieldValue(model, this.value));
				data.push("label", ClassUtil.getFieldValue(model, this.title));
				items.add(data);
			}
		}
		return items;
	}
	/**
	 * 转换成 List<DisplayItem>
	 * 
	 *  Exception
	 */
	public List<DisplayItem> toItems() throws Exception{
		List<DisplayItem> items= new ArrayList<>();
		if(!StringUtil.isSpace(dictType)) {
			Map<String,String> map=DictionaryUtil.get(dictType);
			for(String key : map.keySet()) {
				DisplayItem data=DisplayItem.value(key,map.get(key));
				items.add(data);
			}
		}else {
			Map<String,String> map= new HashMap<>();
			if(!StringUtil.isSpace(this.titleExpression)) map.put(this.title,this.titleExpression);
			List<Model> models=ModelQueryList.getModelList(clazz, 
				new String[] {this.title,this.value},page,
                    !cond.isEmpty() ?QueryCondition.and(cond.toArray(new QueryCondition[]{})):null,
				map,order);
			for(Model model : models) {
				DisplayItem data=DisplayItem.value(
					String.valueOf(ClassUtil.getFieldValue(model, this.value)),
					String.valueOf(ClassUtil.getFieldValue(model, this.title))
				);
				items.add(data);
			}
		}
		return items;
	}
	
}
